package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.service.system.ModuleService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;


@Controller()
@RequestMapping("system/module")
public class ModuleController extends BaseController {
    @Autowired
    private ModuleService moduleService;
    @RequestMapping(value = "/list",name = "分页查询所有的数据")
    //分页查询所有的数据
    public String findAll(@RequestParam(name = "page",defaultValue = "1")int pageNum,@RequestParam(defaultValue = "5")int pages){
        PageInfo<Module> page = moduleService.findPage(pageNum,pages);
        request.setAttribute("page",page);
        return "system/module/module-list";
    }
    @RequestMapping(value = "/toAdd",name = "跳转到新建模块界面")
    //跳转到新建界面
    public String toAdd(){
        List<Module> list = moduleService.findAll();
        request.setAttribute("menus",list);
        return "system/module/module-add";
    }
    @RequestMapping(value = "/edit",name = "创建模块界面")
    //添加数据
    public String edit(Module module){
        if(StringUtils.isBlank(module.getId())) {
            module.setId(UUID.randomUUID().toString());
            moduleService.insert(module);
        }else {
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }
    @RequestMapping(value = "toUpdate",name = "跳转到更新界面,并回显数据")
    //跳转到更新界面,并回显数据
    public String toUpdate(String id){
        List<Module> list=moduleService.findAll();
        request.setAttribute("menus",list);
        return "system/module/module-add";
    }
    @RequestMapping(value = "/delete",name = "删除指定模块")
    public String delete(String id){
        moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }
}
